import $ from "jquery";
import api from './api';
export default {
  data() {
    return {
      data: null,
    };
  },
  mounted() {
    $.ajax({
      url: api.getTemplateData,
      method: "post",
      data: {
        id: this.getQueryString("id") || 0,
        tempId: this.getQueryString("tempId") || this.$options.name.slice(8),
        is_contact: this.getQueryString("is_contact") || 1,
        scale: this.getQueryString("scale") || 0
      }
    }).done(resp => {
      this.data = resp.data;
      this.$nextTick(() => {
        this.fetchDataEnd && this.fetchDataEnd()
      })
    });
  },
  methods:{
    getQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg);
      if (r !== null) return decodeURIComponent(r[2]);
      return null;
    },
    fetchDataEnd(){
      console.log('完成')
    },
    rawhtml(val) {
      return val.replace(/(^\r\n$)|(\n)/g, '<br/>');
    },
    isArray(o){
      return Object.prototype.toString.call(o) == "[object Array]"
    },
    slice(array, start, end) {
      let length = array == null ? 0 : array.length
      if (!length) {
        return []
      }
      start = start == null ? 0 : start
      end = end === undefined ? length : end
    
      if (start < 0) {
        start = -start > length ? 0 : (length + start)
      }
      end = end > length ? length : end
      if (end < 0) {
        end += length
      }
      length = start > end ? 0 : ((end - start) >>> 0)
      start >>>= 0
    
      let index = -1
      const result = new Array(length)
      while (++index < length) {
        result[index] = array[index + start]
      }
      return result
    },
    chunk(array, size) {
      size = Math.max(size, 0)
      const length = array == null ? 0 : array.length
      if (!length || size < 1) {
        return []
      }
      let index = 0
      let resIndex = 0
      const result = new Array(Math.ceil(length / size))
    
      while (index < length) {
        result[resIndex++] = this.slice(array, index, (index += size))
      }
      return result
    }
  }
}